<template>
    <main-wrap style="background: #fcf7ee;">
        <div class="release-from">
            <div class="fromItem">
                <div class="name">发布位置</div>
                <div>
                    <position-select v-model="publishingLocation" :checkArr="checkArr"/>
                </div>
            </div>
            <div class="fromItem">
                <div class="name">标题</div>
                <!-- <input type="text" placeholder="请输入问题简要描述" v-model="fmData.title"/> -->
                <van-field v-model="fmData.title" placeholder="请输入问题简要描述(限制20个字)" maxlength="20" show-word-limit/>
            </div>
            <div class="fromItem">
                <div class="name">问题分类</div>
                <div>
                    <category-select v-model="classfly"/>
                </div>
            </div>
            <div class="fromItem">
                <div class="name">问题详情</div>
                <textarea placeholder="输入问题详情~" v-model="fmData.questionContent"></textarea>
            </div>

            <div class="fromItem">
                <div class="name">上传附件</div>
                <div class="annexBox">
                    <upload-file v-model="fileLists"/>
                </div>
            </div>
        </div>
        <template v-slot:footer>
            <div class="releaseBtn">
                <Button text="立即发布" @click="save" :disabled="disabledFlag"></Button>
            </div>
        </template>
        <perfect-info v-if="pfFlag" :userData='userInfo' v-model:show="pfFlag"/>
    </main-wrap>
</template>

<script>
import { mapGetters } from "vuex";
import { Toast,Icon as vanIcon,Dialog,Field as vanField} from 'vant';
import mainWrap from 'cps/public/mainWrap'
import Button from 'cps/public/Button'
import categorySelect from './components/categorySelect'
import positionSelect from './components/positionSelect'
import uploadFile from './components/uploadFile'
import perfectInfo from 'cps/public/perfectInfo'
import {getQuestionDetail,editQuestion,addQuestion,getCustomList,getUserInfo,sendTemplateMsg,getCompanyList,getTeacherList,sendRemindMsg} from '@/api/index.js'
export default {
    components:{
        mainWrap,
        Button,
        categorySelect,
        positionSelect,
        uploadFile,
        vanField,
        perfectInfo
    },
    data(){
        return{
            fmData:{privacy:1},
            classfly:'',
            querys:{}, // url参数集合
            publishingLocation:'',
            fileLists:[],
            checkArr:[],
            disabledFlag:false,
            userInfo:{},
            pfFlag:false
        }
    },
    computed: {
        ...mapGetters({
             //userInfo:'user/userInfo',
        }),
    },
    async created(){
        this.querys = this.$route.query  // 参数集合:本文组件中用到的参数：问题qid、小程序openid、成员公司curCompanyId、成员公司母公司motherCompanyId、成员公司服务主curServiceId
        console.log("this.querys:",this.querys)
        if(this.querys.qid){
            // 获取问题详情
            const res = await getQuestionDetail(this.querys.qid,this.querys.uid)
            if(res.code == 200){
                this.fmData.questionContent = res.data.questionContent
                this.fmData.title = res.data.title
                this.fileLists = res.data.annex || []
                this.classfly = (res.data.classifyIds && res.data.classifyIds.length>0)?res.data.classifyIds[0]._id:''
                this.checkArr = res.data.publishingLocations
            }
        }
        const res = await getUserInfo(this.querys.uid)
        if(res.code == 200){
            this.userInfo = res.data
           // console.log("44444:",this.userInfo)
            if(!this.userInfo.phone || !this.userInfo.name){
                this.pfFlag = true
            }
        }
    },
    mounted(){
        this.$nextTick(()=>{
            var script = document.createElement('script');
            script.type = 'text/javascript';
            script.src = '/js/uni.webview.1.5.2.js';
            document.body.appendChild(script);
        })
    },
    methods:{  
        async save(){
            if(this.disabledFlag) return
            if(!this.querys.qid && !this.publishingLocation){
                return Toast('请选择发布位置')
            }else if(!this.fmData.title){
                return Toast('请输入标题')
            }else if(!this.classfly){
                return Toast('请选择问题分类')
            }else if(!this.fmData.questionContent){
                return Toast('请输入问题详情')
            }

            this.disabledFlag = true
            
            this.fmData.classifyIds = [this.classfly]
            if(this.publishingLocation){
                this.fmData.publishingLocations = [this.publishingLocation]
            }
            
            
            if(this.querys.qid){

                this.editSumbit()
            }else{
                this.addSumbit()
            }
        },
        async editSumbit(){
            this.fmData.annex = this.fileLists
            const res = await editQuestion({
                id:this.querys.qid,
                title:this.fmData.title,  // 微信验证添加的字段
                content:this.fmData.questionContent,  // 微信验证添加的字段
                openid:this.querys.openid,  // 微信验证添加的字段
                scene:3,
                params:this.fmData
            })
            if(res.code == 200){
                Toast('修改成功')
                uni.navigateBack({delta:1}) // 返回
            }else{
                Toast('修改失败，请重试！')
            }
            this.disabledFlag = false
        },
        async addSumbit(){
            if(this.querys.curCompanyId){ // 用户当前所在的公司
                this.fmData.customerCompanyId = this.querys.curCompanyId
            }
            if(this.querys.curCompanyId){ // 用户当前所在公司的母公司
                this.fmData.customerMotherCompanyId = this.querys.motherCompanyId
            }
            if(this.querys.curServiceId){// 用户当前所在公司的服务公司
                this.fmData.serviceCompanyId = this.querys.curServiceId
            }
            if(this.querys.appUserType==1){ // 普通游客
                delete this.fmData.customerCompanyId 
                delete this.fmData.customerMotherCompanyId 
            }
            // 附件
            if(this.fileLists.length>0){
                this.fmData.annex = this.fileLists
            }
            const res = await addQuestion({
                userId:this.querys.uid,
                from:'EJConsult',
                content:this.fmData.questionContent,  // 微信验证添加的字段
                openid:this.querys.openid,  // 微信验证添加的字段
                scene:3,
                ...this.fmData
            })
            if(res.code == 200){
                let msg = '发布成功，等待专家回复！'
                if(this.querys.appUserType==3){ // 专家发布问题
                    msg = '问题发布成功'
                }   
                // //Toast(msg)
                // if(this.querys.appUserType==2 || this.querys.appUserType==4){ // 企业成员/专属游客 咨询时，发送模板消息
                //     await this.get_taecher_openid2(res.data._id) // 获取教师的ej在线公众号openid，发送模板消息
                // }else if(this.querys.appUserType==3 || this.querys.appUserType==1){ // 专家发表问题 或者普通游客发布问题
                //     await this.get_all_teacher(res.data._id)
                // }

                // 后台发送消息
                await sendRemindMsg({
                    //from:'EJConsult',
                    qustionId:res.data._id,
                    userId:this.querys.uid,
                    appUserType:this.querys.appUserType,
                    curCompanyId:this.querys.curCompanyId,
                    curServiceId:this.querys.curServiceId || '65b0cda56eb6493b2c0ae1b1',
                    title:this.fmData.title,
                    content:this.fmData.questionContent,
                    organizationId:'606bc5cc6418f1256d89f228'
                })

                Dialog.alert({
                    title: '提示信息',
                    message: msg,
                }).then(() =>{
                    //uni.$emit('refreshIndex')  // 刷新主页数据
                    uni.navigateBack({delta:1}) // 返回
                })
                
            }else if(res.code == 8000){
                Toast('发布的内容不合法，请重新输入！')
            }else{
                Toast('发布失败，请重试！')
            }
            this.disabledFlag = false
        },
  
    }
}
</script>

<style lang="scss" scoped>
    .release-from{
       .fromItem{
           padding:0 25px 50px 25px;
           margin: 25px 0 0 0;
           .name{
                display: flex;
                align-items: center;
                font-size: 28px;
                font-weight: bold;
                margin: 0 0 10px 0;
                &::before{
                    content: '';
                    display: block;
                    height: 24px;
                    width: 6px;
                    background: #e3af2f;
                    margin: 0 10px 0 0;
                }
           }
           input{
               width: 100%;
               height: 80px;
               line-height: 80px;
               padding: 20px;
               border: 0;
               background: #fff;
               border-radius: 10px;
           }
           textarea{
               border: 0;
               width: 100%;
               padding: 20px;
               background: #fff;
               height: 300px;
               border-radius: 10px;
           }
           .annexBox{
                padding: 15px 0 0 0;
           }
       } 
       .classifyBox{
           padding:0 25px;
           margin: 25px 0 0 0;
           h4{
               font-size: 34px;
           }
           .classifyList{
               padding: 25px 0 0 0;
               span{
                   float: left;
                   height: 64px;
                   line-height: 64px;
                   text-align: center;
                   border:1px solid #cccccc;
                   border-radius: 32px;
                   padding: 0 20px;
                   margin: 0 20px 20px 0;
                   &.selected{
                       background: #e3af2f;
                       color: #fff;
                   }
               }
           }
           .xzzj{
               margin: 10px -20px 0 -20px;
               .choise{
                   margin: 20px 0 0 20px;
                   width: 150px;
                   height: 150px;
                   border: 1px dashed #dbdada;
                   display: flex;
                   justify-content: center; 
                   align-items: center;
               }
           }
       }
    }
    .releaseBtn{
        padding: 20px 30px;
        background: #f2f2f2;
    }
</style>
